package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

import static com.dkd.common.constant.DkdContants.VM_STATUS_NODEPLOY;

/**
 * 设备Service业务层处理
 * 
 * @author blank
 * @date 2025-07-21
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private IVmTypeService vmTypeService;
    @Autowired
    private IChannelService channelService;
    @Autowired
    private INodeService nodeService;
    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param vendingMachine 设备
     * @return 设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
//        1、新增设备
//        1-1生成8位编号，补充货道编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode); //设备编号
//        1-2查询信息机类型，补充设备容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
//        1-3查询点位表，补充 区域、点位、合作商等信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        //批量添加工具包，批量添加(两个表添加的字段名称要保持一致)：商圈类型，合作商信息，区域信息
        BeanUtil.copyProperties(node, vendingMachine, "id");
        vendingMachine.setAddr(node.getAddress());//设备地址字段名称不一样，需要单独添加
//        设备状态插入
        vendingMachine.setVmStatus(VM_STATUS_NODEPLOY);
//        获取创建时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
//        获取更新时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
//        1-5保存设备信息
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
//2.新增货道信息
//        2.1声明货道集合
        List<Channel> channelList = new ArrayList<>();
//        2-2双层循环
        for (int i = 1; i <= vmType.getVmRow(); i++) {//外层行循环
            for (int j = 1; j <= vmType.getVmCol(); j++) { //内层列循环
                //2-3封装channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);//货道编号
                channel.setVmId(vendingMachine.getId());//设备id
                channel.setInnerCode(vendingMachine.getInnerCode());// 售货机编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());//货道容量
                channel.setCreateTime(DateUtils.getNowDate());//创建时间
                channel.setUpdateTime(DateUtils.getNowDate());//更新时间
                channelList.add(channel);

            }
        }
//        2-4批量新增
        channelService.batchInsertChannel(channelList);

        return result;
    }

    /**
     * 修改设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        if (vendingMachine.getNodeId() != null){
            //1-3查询点位表，补充 区域、点位、合作商等信息
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node, vendingMachine, "id");    //批量添加(两个表添加的字段名称要保持一致)：商圈类型，合作商信息，区域信息
            vendingMachine.setAddr(node.getAddress());//设备地址字段名称不一样，需要单独添加
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());//获取更新时间
        return vendingMachineMapper.updateVendingMachine(vendingMachine);//修改设备信息
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
    /**
     * 根据设备编号查询设备信息
     * @param innerCode
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {

        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
